package com.codeczx.completeuidemo;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by codeczx on 2017/7/5.
 */

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.MyViewHolder>{

    private static final String TAG = "RVAdapter";
    private List<String> mList;

    public RVAdapter(List<String> list){
        mList = list != null ? list : new ArrayList<String>();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Log.i(TAG, "onBindViewHolder: "+mList.get(position));
        holder.tv.setText(mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void updateData(List<String> list){

        if (list != null) {
            Log.i(TAG, "updateData: list.size()"+list.size());
            mList = list;
            Log.i(TAG, "updateData: mList.size()"+mList.size());
            notifyDataSetChanged();
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
        }
    }
}
